/*
 * ViewTableRow.cpp
 *
 *  Created on: 2011-1-8
 *      Author: Administrator
 */

#include "ViewTableRow.h"
#include "ListBox.h"
#include "ViewTable.h"
#include "ViewTableData.h"

CViewTableRow::CViewTableRow()
	{
	iRowData = NULL;
	}

CViewTableRow::~CViewTableRow()
	{
	iRowData = NULL;//not own
	}

CViewTableRow* CViewTableRow::NewL(const CListBox* aOwner,TViewTableHeadRowData* aRowData)
	{
	CViewTableRow* self = CViewTableRow::NewLC(aOwner,aRowData);
    CleanupStack::Pop(self);
    return self;
	}

CViewTableRow* CViewTableRow::NewLC(const CListBox* aOwner,TViewTableHeadRowData* aRowData)
	{
	CViewTableRow* self = new(ELeave) CViewTableRow();
    CleanupStack::PushL(self);
    self->ConstructL(aOwner,aRowData);
    return self;
	}

void CViewTableRow::ConstructL(const CListBox* aOwner,TViewTableHeadRowData* aRowData)
	{
	iOwner = aOwner;
	iRowData = aRowData;
	iTextAlign = CGraphicsContext::ECenter;
	ActivateL();
	}

void CViewTableRow::setColumnWidths(RArray<TInt> aColumnWidths)
	{
	if (iRowData)
		iRowData->setColumnWidths(aColumnWidths);
	}

void CViewTableRow::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc, aRect);
	if (iRowData && iRowData->Count() > 1)
		{
		TInt count = iRowData->Count();
		gc.UseFont(iTitleFont);
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		TInt baseline = aRect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
		TPoint pt1 = aRect.iTl;
		TPoint pt2 = TPoint(aRect.iTl.iX,aRect.iBr.iY);
		gc.SetPenColor(KRgbWhite);
		gc.DrawLine(pt1,pt2);
		TRect textRect;
		textRect.iTl = aRect.iTl;
		textRect.iBr.iX = aRect.iTl.iX;
		textRect.iBr.iY = aRect.iBr.iY;
		TInt columnWidth = 0;
		for (TInt i = 0; i < count; i++)
			{
			columnWidth = iRowData->getColumnWidth(i);
			textRect.iBr.iX += columnWidth;
			gc.SetPenColor(iTitleColor);
			gc.DrawText(iRowData->getData(i), textRect, baseline, iTextAlign,2);
			pt1.iX += columnWidth;
			pt2.iX += columnWidth;
			gc.SetPenColor(KRgbWhite);
			gc.DrawLine(pt1,pt2);
			textRect.iTl.iX += columnWidth;
			}
		if (iOwner && iOwner->ScrollBar() && !iOwner->ScrollBar()->Visible())
			{
			pt1.iX = aRect.iBr.iX - 1;
			pt2.iX = aRect.iBr.iX - 1;
			gc.SetPenColor(KRgbWhite);
			gc.DrawLine(pt1,pt2);
			}
		gc.DiscardFont();//dicard the font
		}
    if (iOwner && iOwner->DrawLine())
    	{
		TInt gap = 0;
		if (iOwner->ScrollBar() && iOwner->ScrollBar()->Visible())
			gap = 6;
		CGraphicsTool::DrawLine(gc,TPoint(aRect.iTl.iX,aRect.iBr.iY - 1),TPoint(aRect.iBr.iX - gap,aRect.iBr.iY - 1),KRgbWhite);
    	}
	}

void CViewTableRow::SetLayout()
	{
	TInt height = iTitlePosition.iY + iTitleFont->HeightInPixels() + 5;
	if (height < 25)
		{
		height = 25;
		TRect rect(0, 0, iOwner->Rect().Width(), height);
		SetRect(rect);
		}
	}

TViewTableHeadRowData* CViewTableRow::getRowData()
	{
	return iRowData;
	}
